$(function () {
	 $.get("../personnel/grouppersonnel/list", function(r){
			vm.groupPersonnel = r.listGroupPersonnel;
	        var list = r.listGroupPersonnel;
	        var html='';
	        html+='<option value="0">请选择分组</option>';
	        $.each(list,function(index,item){
	        	
	        	html+='<option value="'+item.id+'">'+item.name+'</option>';
	        	
	        });
	        $("#liebiao").html(html);
	        
	    });
	    
	    $.get("../personnel/crossborderalarm/list", function(r){
	    	vm.crossborderalarm = r.listCrossborderalarm;
	    
	    	
	    });
	    
	    $.get("../personnel/disappearalarm/list", function(r){
	    	vm.disappearalarm = r.listDisappearalarm;
	    
	    	
	    });
	
	
    $("#jqGrid").jqGrid({
        url: '../personnel/list',
        datatype: "json",
        colModel: [			
{ label: '名字', name: 'name', width: 80 }, 			
{ label: '身份证', name: 'nric', width: 80 }, 			
{ label: '联系方式', name: 'contact', width: 80 }, 			
{ label: '标签', name: 'codeTag', width: 80 }, 			
{ label: '组别', name: 'idGroupPersonnel', width: 80,formatter:customFmatter1}, 			
{ label: '越界策略', name: 'idCrossborderalarm', width: 80,formatter:customFmatter}, 			
{ label: '离岗策略', name: 'idDisappearalarm', width: 80,formatter:customFmatter2 }				
        ],
		viewrecords: true,
		   height: 'auto',
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
	
   
});


	
	function customFmatter1(cellvalue, options, rowObject){ 
		
		var name;
		
		$.each(vm.groupPersonnel,function(index,item){
			
			if(cellvalue==item.id){
				
				name=item.name;
			}
			
		});
	
	
	return name;  
}

function customFmatter(cellvalue, options, rowObject){ 
	
	var name;
	
	  $.each(vm.crossborderalarm,function(index,item){
      	
		  if(cellvalue==item.id){
				
				name=item.name;
			}
      	
      });
	  

    return name;  
}

function customFmatter2(cellvalue, options, rowObject){ 

var name;

$.each(vm.disappearalarm,function(index,item){
	
	if(cellvalue==item.id){
		//alert(name)
		name=item.name;
	}
	

});

return name;

}

function show_sub(v){   

	vm.showList = true;
	var page = $("#jqGrid").jqGrid('getGridParam','page');
	$("#jqGrid").jqGrid('setGridParam',{
		  postData:{'idGroupPersonnel': v},
        page:page
    }).trigger("reloadGrid");
} 

function getval(){
	vm.showList = true;
	var page = $("#jqGrid").jqGrid('getGridParam','page');
	$("#jqGrid").jqGrid('setGridParam',{
		  postData:{'str': $("#val").val()},
        page:page
    }).trigger("reloadGrid");
}
//导入文件
function uploadFile(){
	var path;
	var id=document.getElementById("upload-file");
	//console.log(id.value);
	var value=id.value;
	
//	alert(value);
	var str=value.split("\\");
	var newStr="";
	$.each(str,function(index,item){
		console.log(item);
		if(index!=str.length-1){
	newStr+=item+"\\\\";
		}else{
			newStr+=item;
		}
	});
	//console.log(newStr);
	document.getElementById("start-upload").href="../personnel/export?path="+value;
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		personnel: {},
		groupPersonnel:{},
		crossborderalarmdetal: {},
		disappearalarm:{},
		crossborderalarm:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.personnel = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.personnel.id == null ? "../personnel/save" : "../personnel/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.personnel),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../personnel/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../personnel/info/"+id, function(r){
                vm.personnel = r.personnel;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
                
            }).trigger("reloadGrid");
		}
	}
});